package br.com.registrofinancas.util;

import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

public class GPSUtils {
	
    public static Location getGPSPosition(Context contexto) {
    	Location local = null;
    	LocationManager locationManager = (LocationManager) contexto.getSystemService(Context.LOCATION_SERVICE);
    	try {
            Criteria criteria = new Criteria();
            String bestProvider = locationManager.getBestProvider(criteria, false);
            if (bestProvider != null) {
            	local = locationManager.getLastKnownLocation(bestProvider);
            }
            if (local == null) {
            	List<String> providers = locationManager.getProviders(true);
            	for (String provider : providers) {
            		Location localTemp = locationManager.getLastKnownLocation(provider);
            		if (localTemp == null) {
            			continue;
            		}
            		if (local == null || localTemp.getAccuracy() < local.getAccuracy()) {
            			local = localTemp;
            		}
            	}
            }
        } catch (Exception ex) {

        }
    	return local;
    }	
}
